// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ImageFrameConcatAgentTest.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Test.Robotics.Vision
{
    using System;
    using System.Drawing;
    using System.Runtime.Serialization;
    using System.Threading;
    using Microsoft.Robotics.Numerics;
    using Microsoft.Robotics.Runtime;
    using Microsoft.Robotics.Vision;
    using Microsoft.Robotics.Vision.Cameras;
    using Microsoft.Robotics.Vision.Runtime;
    using Microsoft.Robotics.Vision.Runtime.Cameras;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Test.Robotics.Runtime;

    /// <summary>
    /// Image concatenation agent tests
    /// </summary>
    [TestClass]    
    public class ImageFrameConcatAgentTest
    {
        /// <summary>
        /// Send messages to concatenation agent - no fail
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void ImageFrameConcatAgentTestNormalCase()
        {
            Pose[] msgPose = new Pose[] { new Pose(new Vector3(0, 0, 0)), new Pose(new Vector3(0, 0.15, 0)), new Pose(new Vector3(0.1, 0, 0)), new Pose(new Vector3(0.1, 0.15, 0)) };

            int messageCount = this.RunAgentTest(msgPose);
            Assert.IsTrue(messageCount > 0, "No message received");
        }

        /// <summary>
        /// Send messages to concatenation agent - fail case
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        [ExpectedException(typeof(ArgumentException))]
        [Ignore]
        public void ImageFrameConcatAgentTestFailureCase()
        {
            Pose[] msgPose = new Pose[] { new Pose(new Vector3(0, 0, 0)), new Pose(new Vector3(0, 0.15, 0)), new Pose(new Vector3(0.1, 0, 0)), new Pose(new Vector3(0.1, 0.17, 0)) };

            int messageCount = this.RunAgentTest(msgPose);
        }

        /// <summary>
        /// Runs a concatenation agent test with supplied poses
        /// </summary>
        /// <param name="msgPose">Poses to add</param>
        /// <returns>Number of received messages at end of pipeline (should be 1)</returns>
        private int RunAgentTest(Pose[] msgPose)
        {
            int messageCount = 0;
            AgentLocator locator = new AgentLocator();

                MessageInjectorAgent<RGBCameraAgentMessage> rgb1 = new MessageInjectorAgent<RGBCameraAgentMessage>("rgb1");
                IAgentHost rgb1Host = locator.Register(rgb1);

                MessageInjectorAgent<RGBCameraAgentMessage> rgb2 = new MessageInjectorAgent<RGBCameraAgentMessage>("rgb2");
                IAgentHost rgb2Host = locator.Register(rgb2);

                MessageInjectorAgent<RGBCameraAgentMessage> rgb3 = new MessageInjectorAgent<RGBCameraAgentMessage>("rgb3");
                IAgentHost rgb3Host = locator.Register(rgb3);

                MessageInjectorAgent<RGBCameraAgentMessage> rgb4 = new MessageInjectorAgent<RGBCameraAgentMessage>("rgb4");
                IAgentHost rgb4Host = locator.Register(rgb4);

                RGBImageFrameConcatAgent concat = new RGBImageFrameConcatAgent(
                            name: "concat",
                            producers: new string[] { "rgb1", "rgb2", "rgb3", "rgb4" },
                            countX: 2,
                            countY: 2,
                            enableCameraMapCache: true);
                IAgentHost concatHost = locator.Register(concat);

                MessageCollectorAgent<RGBCameraAgentMessage> collector =
                    new MessageCollectorAgent<RGBCameraAgentMessage>("collector", concat);

            IAgentHost collectorHost = locator.Register(collector);

            try
            {
                rgb1Host.Activate();
                rgb2Host.Activate();
                rgb3Host.Activate();
                rgb4Host.Activate();
                concatHost.Activate();
                collectorHost.Activate();

                Rectangle[] boundaries = new Rectangle[] { new Rectangle(10, 15, 5, 10), new Rectangle(5, 7, 5, 16), new Rectangle(10, 10, 10, 10), new Rectangle(2, 2, 10, 16) };

                for (int i = 3; i >= 0; --i)
                {
                    ImageFrameYuy2 frame = new ImageFrameYuy2(30, 30);

                    RGBCameraAgentMessage msg = new RGBCameraAgentMessage(0, frame, msgPose[i], boundaries[i], string.Format("rgb{0}", i + 1));
                    rgb1.Publish(msg);
                }

                // Wait for the message to appear at end of pipeline
                collector.Wait(2000);
                messageCount = collector.Results.Count;
            }
            finally
            {
                rgb1Host.Deactivate();
                rgb2Host.Deactivate();
                rgb3Host.Deactivate();
                rgb4Host.Deactivate();
                concatHost.Deactivate();
                collectorHost.Deactivate();
                locator.Deactivate();
            }

            return messageCount;
        }
    }
}
